package com.mainbo.androidframework.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * thread controll utils
 * Created by lsmtty on 2016/3/14.
 */
public class ThreadPoolManager {
    private volatile static  ThreadPoolManager threadPoolManager;
    private static ExecutorService fixExecutorService;
    private static ExecutorService cachedExecutorService;

    private ThreadPoolManager(){
        fixExecutorService = Executors.newFixedThreadPool(10);
        cachedExecutorService = Executors.newCachedThreadPool();
    }
    public static ThreadPoolManager getInstance(){
        if(threadPoolManager==null){
            synchronized (ThreadPoolManager.class){
                if(threadPoolManager==null){
                    threadPoolManager = new ThreadPoolManager();
                }
            }
        }
        return threadPoolManager;
    }

    /**
     * execute runnable in fix number thread pool(10)
     * @param runnable
     */
    public void fixThreadExecute(Runnable runnable){
        fixExecutorService.execute(runnable);
    }

    /**
     * execute runnable in cached ThreadPool
     * @param runnable
     */
    public void cachedThreadExecute(Runnable runnable){
        cachedExecutorService.execute(runnable);
    }
}
